﻿using System;
using System.Drawing;
namespace HowToUsePie
{
    public class Mypie
    {
        private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
        private Bitmap objBitmap; //位图对象
        private int m_Width;//整个图像的宽度
        private int m_Height; //整个图像的高度
        private string m_Title; //图像的标题
        private string m_Unit;//图像图例的单位
        private string[] m_Keys; //图例的内容
        private float[] m_Values; //图例的取值
        /// <summary>
        /// 饼图的相关属性值
        /// </summary>
        /// <param name="Width">图像宽度</param>
        /// <param name="Height">图像高度</param>
        /// <param name="Title">图像标题</param>
        /// <param name="Unit">图例单位</param>
        /// <param name="Keys">图例内容</param>
        /// <param name="Values">图例取值</param>
        /// <returns></returns>
        public void SetParameter(int Width, int Heigth, String Title, string Unit, String[] Keys, float[] Values)
        {
            this.m_Width = Width;
            this.m_Height = Heigth;
            this.m_Title = Title;
            this.m_Unit = Unit;
            //键
            this.m_Keys = Keys;
            //值
            this.m_Values = Values;
        }
        private Color m_BgColor = Color.Snow; //整体图片的背景颜色
        private Color m_TextColor = Color.Black; //文字颜色
        private Color m_BorderColor = Color.Black; //整体边框颜色
        //Pie颜色数组
        private Color[] m_PieColor = new Color[] { Color.Green, Color.Red, Color.Yellow, Color.Blue, Color.Orange, Color.Aqua, Color.SkyBlue, Color.DeepPink, Color.Azure, Color.Brown };

        //设置宽度
        public int Width
        {
            set
            {
                if (value < 300)
                {
                    m_Width = 300;
                }
                else
                {
                    m_Width = value;
                }
            }
            get
            {
                return m_Width;
            }
        }
        //设置高度
        public int Height
        {
            set
            {
                if (value < 300)
                {
                    m_Height = 300;
                }
                else
                {
                    m_Height = value;
                }
            }
            get
            {
                return m_Height;
            }
        }
        //设置标题
        public string Title
        {
            set
            {
                m_Title = value;
            }
            get
            {
                return m_Title;
            }
        }
        //设置图例单位
        public string Unit
        {
            set
            {
                m_Unit = value;
            }
            get
            {
                return m_Unit;
            }
        }
        public string[] Keys
        {
            set
            {
                m_Keys = value;
            }
            get
            {
                return m_Keys;
            }
        }
        public float[] Values
        {
            set
            {
                m_Values = value;
            }
            get
            {
                return m_Values;
            }
        }
        public Color BgColor
        {
            set
            {
                m_BgColor = value;
            }
            get
            {
                return m_BgColor;
            }
        }
        public Color TextColor
        {
            set
            {
                m_TextColor = value;
            }
            get
            {
                return m_TextColor;
            }
        }
        public Color BorderColor
        {
            set
            {
                m_BorderColor = value;
            }
            get
            {
                return m_BorderColor;
            }
        }
        public Color[] PieColor
        {
            set
            {
                m_PieColor = value;
            }
            get
            {
                return m_PieColor;
            }
        }
        private Color GetColor(int i) //分配各Pie的颜色
        {
            try
            {
                if (0 < i && i < PieColor.Length)
                {
                    return PieColor[i];
                }
                else if (i >= PieColor.Length)
                {
                    int j = i % PieColor.Length;
                    return PieColor[j];
                }
                else
                {
                    return Color.Green;
                }
            }
            catch
            {
                return Color.Green;
            }
        }
        //生成图像并返回bmp图像对象
        public Bitmap CreateImage()
        {
            InitializeGraph();
            DrawRight(ref objGraphics);
            DrawContent(ref objGraphics);
            return objBitmap;
        }
        //初始化和填充图像区域，画出边框，初始标题
        private void InitializeGraph()
        {
            //根据给定的高度和宽度创建一个位图图像
            objBitmap = new Bitmap(Width, Height);
            //从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
            objGraphics = Graphics.FromImage(objBitmap);
            //根据给定颜色(LightGray)填充图像的矩形区域 (背景)
            objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width, Height);
            objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2);
            //初始化标题
            CreateTitle(ref objGraphics);
        }
        //初始化右边说明部分
        private void DrawRight(ref Graphics objGraphics)
        {
            objGraphics.DrawString(String.Format("单位：{0}", Unit), new Font("宋体", 10), new SolidBrush(TextColor), Width - 95, 30);
            Point KeysPoint = new Point(Width - 120, 50);
            Point KeysTextPoint = new Point(Width - 90, 50);
            for (int i = 0; i < Keys.Length; i++)
            {
                objGraphics.DrawRectangle(new Pen(BorderColor, 1), KeysPoint.X, KeysPoint.Y, 21, 11);
                objGraphics.FillRectangle(new SolidBrush(GetColor(i)), KeysPoint.X, KeysPoint.Y, 20, 10);
                objGraphics.DrawString(String.Format("{0} {1}", Keys[i], Values[i]), new Font("宋体", 10), new SolidBrush(TextColor), KeysTextPoint);
                KeysPoint.Y += 15;
                KeysTextPoint.Y += 15;
            }
        }
        private void DrawContent(ref Graphics objGraphics)
        {
            float Sum = 0;
            float StartAngle = 0;
            float SweepAngle = 0;
            float CircleHeight = 0;
            float CircleX = 0;
            float CircleY = 0;
            for (int i = 0; i < Values.Length; i++)
            {
                Sum += Values[i];
            }
            if (Width > Height)
            {
                CircleHeight = Height - 100;
            }
            else
            {
                CircleHeight = Width - 150;
            }
            CircleX = (Width - 150) / 2 - CircleHeight / 2;
            CircleY = Height - 50 - CircleHeight;
            for (int i = 0; i < Values.Length; i++)
            {
                SweepAngle = (Values[i] / Sum) * 360;
                objGraphics.DrawPie(new Pen(BorderColor, 1), CircleX, CircleY, CircleHeight, CircleHeight, StartAngle, SweepAngle);
                objGraphics.FillPie(new SolidBrush(GetColor(i)), CircleX, CircleY, CircleHeight, CircleHeight, StartAngle, SweepAngle);
                StartAngle += SweepAngle;
            }
        }
        //初始化标题
        private void CreateTitle(ref Graphics objGraphics)
        {
            objGraphics.DrawString(Title, new Font("宋体", 16), new SolidBrush(TextColor), new Point(5, 5));
        }
    }
}